import datetime

from fastapi import APIRouter

from app.allsubdb.wanfangqk.models import WFJournalPageIdModel, WfJournalPageId, WFJournalIssueModel, WfJournalIssue, \
    WfJournalArticleInfoModel, WfJournalArticleInfo, WfJournalArticle, WfJournalArticleModel, WfJournalRelModel, \
    WfJournalRel, WfJournalRefModel, WfJournalRef, WfJournalRefOld, WfJournalRefOldModel, WfJournalArticleNewModel, \
    WfJournalArticleNew
from app.core.return_info import ReturnInfo, SUCCESS

router = APIRouter()


@router.post("/insert/wfjournalpageid")
def wfjournalpageid(wfjpid: WFJournalPageIdModel):
    return_info = ReturnInfo()
    m_result = WfJournalPageId.objects(cid=wfjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        m_result.update(down_date=wfjpid.down_date,
                        jsondicts=wfjpid.jsondicts,
                        update_time=datetime.datetime.now())
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = WfJournalPageId(cid=wfjpid.cid,
                               down_date=wfjpid.down_date,
                               jsondicts=wfjpid.jsondicts,
                               update_time=datetime.datetime.now(),
                               create_time=datetime.datetime.now()
                               )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()


@router.post("/insert/wfjournalissue")
def wfjournalpageid(wfjpid: WFJournalIssueModel):
    return_info = ReturnInfo()
    m_result = WfJournalIssue.objects(cid=wfjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        m_result.update(down_date=wfjpid.down_date,
                        jsonlists=wfjpid.jsonlists,
                        update_time=datetime.datetime.now())
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = WfJournalIssue(cid=wfjpid.cid,
                              down_date=wfjpid.down_date,
                              jsonlists=wfjpid.jsonlists,
                              update_time=datetime.datetime.now(),
                              create_time=datetime.datetime.now()
                              )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()


@router.post("/insert/wfjournalarticleinfo")
def wfjournalpageid(wfjpid: WfJournalArticleInfoModel):
    return_info = ReturnInfo()
    m_result = WfJournalArticleInfo.objects(cid=wfjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        m_result.update(down_date=wfjpid.down_date,
                        jsondicts=wfjpid.jsondicts,
                        page=wfjpid.page,
                        update_time=datetime.datetime.now())
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = WfJournalArticleInfo(cid=wfjpid.cid,
                                    down_date=wfjpid.down_date,
                                    jsondicts=wfjpid.jsondicts,
                                    page=wfjpid.page,
                                    update_time=datetime.datetime.now(),
                                    create_time=datetime.datetime.now()
                                    )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()



@router.post("/get/wfjournalarticleinfo")
def wfjournalpageid(wfjpid: WfJournalArticleInfoModel):
    return_info = ReturnInfo()
    m_result = WfJournalArticleInfo.objects(cid=wfjpid.cid).first()
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = m_result.to_mongo().to_dict()
        return return_info.todict()  #
    else:
        return_info.status = False
        return_info.msg = "查询无该条数据"
        return_info.data = ""
        return return_info.todict()

@router.post("/insert/wfjournalarticle")
def wfjournalpageid(wfjpid: WfJournalArticleModel):
    return_info = ReturnInfo()
    m_result = WfJournalArticle.objects(cid=wfjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        m_result.update(down_date=wfjpid.down_date,
                        jsondicts=wfjpid.jsondicts,
                        update_time=datetime.datetime.now())
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = WfJournalArticle(cid=wfjpid.cid,
                                down_date=wfjpid.down_date,
                                jsondicts=wfjpid.jsondicts,
                                update_time=datetime.datetime.now(),
                                create_time=datetime.datetime.now()
                                )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()


@router.post("/insert/wfjournalrel")
def wfjournalpageid(wfjpid: WfJournalRelModel):
    return_info = ReturnInfo()
    m_result = WfJournalRel.objects(cid=wfjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        m_result.update(down_date=wfjpid.down_date,
                        jsondicts=wfjpid.jsondicts,
                        update_time=datetime.datetime.now())
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = WfJournalRel(cid=wfjpid.cid,
                            down_date=wfjpid.down_date,
                            jsondicts=wfjpid.jsondicts,
                            update_time=datetime.datetime.now(),
                            create_time=datetime.datetime.now()
                            )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()


@router.post("/insert/wfjournalref")
def wfjournalpageid(wfjpid: WfJournalRefModel):
    return_info = ReturnInfo()
    m_result = WfJournalRef.objects(cid=wfjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        m_result.update(down_date=wfjpid.down_date,
                        rawid=wfjpid.rawid,
                        jsondicts=wfjpid.jsondicts,
                        ref_cnt=wfjpid.ref_cnt,
                        update_time=datetime.datetime.now())
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = WfJournalRef(cid=wfjpid.cid,
                            rawid=wfjpid.rawid,
                            down_date=wfjpid.down_date,
                            jsondicts=wfjpid.jsondicts,
                            ref_cnt=wfjpid.ref_cnt,
                            update_time=datetime.datetime.now(),
                            create_time=datetime.datetime.now()
                            )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()


@router.post("/insert/wfjournalrefold")
def wfjournalpageid(wfjpid: WfJournalRefOldModel):
    return_info = ReturnInfo()
    m_result = WfJournalRefOld.objects(cid=wfjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        m_result.update(down_date=wfjpid.down_date,
                        rawid=wfjpid.rawid,
                        html=wfjpid.html,
                        ref_cnt=wfjpid.ref_cnt,
                        update_time=datetime.datetime.now())
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = WfJournalRefOld(cid=wfjpid.cid,
                               rawid=wfjpid.rawid,
                               down_date=wfjpid.down_date,
                               html=wfjpid.html,
                               ref_cnt=wfjpid.ref_cnt,
                               update_time=datetime.datetime.now(),
                               create_time=datetime.datetime.now()
                               )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()


@router.post("/insert/wfjournalarticlenew")
def wfjournalpageid(wfjpid: WfJournalArticleNewModel):
    return_info = ReturnInfo()
    m_result = WfJournalArticleNew.objects(cid=wfjpid.cid)
    if m_result:  # mongoengine.queryset.queryset.QuerySet
        m_result.update(down_date=wfjpid.down_date,
                        jsondicts=wfjpid.jsondicts,
                        update_time=datetime.datetime.now())
        # jwpp.reload()
        return_info.status = SUCCESS
        return_info.data = {"boos": [str(one_obj.id) for one_obj in m_result]}
        return return_info.todict()  #
    else:
        jwpp = WfJournalArticleNew(cid=wfjpid.cid,
                                   down_date=wfjpid.down_date,
                                   jsondicts=wfjpid.jsondicts,
                                   update_time=datetime.datetime.now(),
                                   create_time=datetime.datetime.now()
                                   )
        jwpp.save()
        return_info.status = SUCCESS
        return_info.data = {"boos": str(jwpp.id)}
        return return_info.todict()
